//
//  ByHomeVC.m
//  hutouben_ios
//
//  Created by mc on 16/11/29.
//  Copyright © 2016年 mc. All rights reserved.
//

#import "ByHomeVC.h"

// vc
#import "ByRoadRepairVC.h"          // 道路救援


// view
#import "RPRingedPages.h"           // 分页视图

@interface ByHomeVC () <RPRingedPagesDelegate, RPRingedPagesDataSource>

// view
@property (nonatomic, strong) RPRingedPages *pages;
@property (nonatomic, strong) UIButton *cityView;

// data
@property (nonatomic, strong) NSMutableArray *items;



@end

@implementation ByHomeVC

#pragma mark - 懒加载
- (RPRingedPages *)pages {
    if (_pages == nil) {
        CGRect pagesFrame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH * 0.5);
        RPRingedPages *pages = [[RPRingedPages alloc] initWithFrame:pagesFrame];
        pages.showPageControl = NO;
        pages.carousel.mainPageSize = CGSizeMake(pages.width * 0.8, pages.height * 0.9);
        pages.carousel.pageScale = 0.9;
        pages.dataSource = self;
        pages.delegate = self;
        
        _pages = pages;
    }
    return _pages;
}
- (NSMutableArray *)items {
    if (_items == nil) {
        _items = [NSMutableArray arrayWithArray:@[@"1", @"2", @"3", @"4", @"5", @"6", @"7",]];
    }
    return _items;
}

#pragma mark - view method
- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"虎头奔";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [self createCitySelectLeftItem];
    [self addBYItemviews:@[@"司机认证"] mode:ByNavigationItemModeText position:ByNavigationItemPositionModeRight];
    
    [self createFuncationView];
    
    [self.view addSubview:self.pages];
    [self.pages reloadData];

}


#pragma mark - 创建视图
- (void)createCitySelectLeftItem {
    
    NSString *text = @"浙江省杭州市平底线";
    CGFloat width = [text getSizeOfString:15].width + 10 + 25;
    self.cityView = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cityView.frame = CGRectMake(0, 0, width, 30);
    [self.cityView setImage:[UIImage imageNamed:@"login_password"] forState:UIControlStateNormal];
    [self.cityView setImageEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 15)];
    self.cityView.titleLabel.font = FONT(15);
    [self.cityView setTitleEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
    [self.cityView setTitle:text forState:UIControlStateNormal];
    [self.cityView setTitleColor:HTB_THEME_COLOR forState:UIControlStateNormal];
    [self.cityView addTarget:self action:@selector(cityTap:) forControlEvents:UIControlEventTouchUpInside];
    
    
    [self addCustomItemviews:@[self.cityView] position:ByNavigationItemPositionModeLeft];
}

- (void)createFuncationView {
    
    CGFloat x, y, width, height;
    CGFloat margin = 8;

    NSArray *images = @[@"index_daoluqiangxiu",
                        @"index_zhaogongzuo",
                        @"index_weixiubaoyang",
                        @"index_ershouche",
                        @"index_qipeishangcheng",
                        @"index_laosiji"];
    NSArray *texts = @[@"道路救援",
                       @"找工作",
                       @"维修保养",
                       @"二手车",
                       @"汽配商城",
                       @"老司机",];
    for (int i = 0; i < texts.count; i ++) {
        width = (SCREEN_WIDTH - 3 * margin) / 2;
        height = (SCREEN_HEIGHT - 64 - 49 - self.pages.bottom - 3 * margin) / 3;
        x = margin + (i % 2) * (width + margin);
        y = self.pages.bottom + (i / 2) * (height + margin);
        UIControl *view = [self createHorizontalModeView:CGRectMake(x, y, width, height)
                                               imageSize:CGSizeMake(height * 0.5, height * 0.5)
                                               imageName:images[i]
                                                textFont:FONT(18)
                                                    text:texts[i]
                                                  margin:10];
        view.layer.cornerRadius = 5;
        view.tag = 1 + i;
        [self.view addSubview:view];
        
        
        [view addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    }

}


// 创建水平排版的视图（可点击）
- (UIControl *)createHorizontalModeView:(CGRect)frame
                              imageSize:(CGSize)size
                              imageName:(NSString *)name
                               textFont:(UIFont *)font
                                   text:(NSString *)text
                                 margin:(CGFloat)margin {
    
    // 1.创建基底视图
    UIControl *basalView = [[UIControl alloc] initWithFrame:frame];
    basalView.backgroundColor = [UIColor whiteColor];
    
    // 2.创建图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    imageView.image = [UIImage imageNamed:name];
    [basalView addSubview:imageView];
    
    // 3.创建文字
    CGFloat fontSize = [font pointSize];
    CGFloat width = [text getSizeOfString:fontSize].width + 10; // 防止iOS10以上不适配
    CGFloat height = basalView.height;
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    textLabel.adjustsFontSizeToFitWidth = YES;
    textLabel.font = font;
    textLabel.text = text;
    textLabel.textColor = TEXT_COLOR;
    textLabel.numberOfLines = 2;
    textLabel.textAlignment = NSTextAlignmentCenter;
    [basalView addSubview:textLabel];
    
    // 4.适配距离
    CGFloat remine = basalView.width - imageView.width - textLabel.width - margin;
    while (remine < 0) {
        textLabel.width /= 2;
        remine = basalView.width - imageView.width - textLabel.width - margin;
    }
    
    imageView.left = remine / 2;
    imageView.y = basalView.height / 2;
    textLabel.left = imageView.right + margin;
    textLabel.y = basalView.height / 2;
    
    return basalView;
}


#pragma mark - 分页视图 delegate
- (NSInteger)numberOfItemsInRingedPages:(RPRingedPages *)pages {
    return self.items.count;
}
- (UIView *)ringedPages:(RPRingedPages *)pages viewForItemAtIndex:(NSInteger)index {
    UILabel *label = (UILabel *)[pages dequeueReusablePage];
    if (![label isKindOfClass:[UILabel class]]) {
        label = [UILabel new];
        label.font = [UIFont systemFontOfSize:50];
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = [UIColor whiteColor];
        label.layer.backgroundColor = [UIColor blackColor].CGColor;
        label.layer.cornerRadius = 10;
    }
    label.text = self.items[index];
    return label;
}
- (void)didSelectedCurrentPageInPages:(RPRingedPages *)pages {
    NSLog(@"pages selected, the current index is %zd", pages.currentIndex);
}
- (void)pages:(RPRingedPages *)pages didScrollToIndex:(NSInteger)index {
    NSLog(@"pages scrolled to index: %zd", index);
}


#pragma mark - 点击
- (void)cityTap:(UIButton *)button {
    
}
- (void)tapAction:(UIControl *)sender {
    if (sender.tag == 1) {
        ByRoadRepairVC *vc = [[ByRoadRepairVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }
    
}

@end
